import { Inject, Get, Post, Body, Query } from '@midwayjs/core';
import { CoolController, BaseController } from '@cool-midway/core';
import { UserProfileProfileEntity } from '../../entity/profile';
import { UserProfileProfileService } from '../../service/profile';
import { UserInfoEntity } from '../../../user/entity/info';

/**
 * 用户档案信息
 */
@CoolController({
  api: ['add', 'delete', 'update', 'info', 'list', 'page'],
  entity: UserProfileProfileEntity,
  service: UserProfileProfileService,
  pageQueryOp: {
    keyWordLikeFields: ['a.name'],
    fieldEq: ['a.gender', 'a.contact', 'a.idCard'],
    select: ['a.*', 'b.nickName AS userName'],
    join: [
      {
        entity: UserInfoEntity,
        alias: 'b',
        condition: 'a.userId = b.id',
        type: 'innerJoin',
      },
    ],
  },
})
export class AdminUserProfileProfileController extends BaseController {
  @Inject()
  userProfileProfileService: UserProfileProfileService;

  /**
   * 获取用户档案信息
   */
  @Get('/getByUserId')
  async getByUserId(@Query('userId') userId: number) {
    const profile = await this.userProfileProfileService.getByUserId(userId);
    if (!profile) {
      return this.fail('用户档案不存在');
    }
    return this.ok(profile);
  }

  /**
   * 添加或更新用户档案信息
   */
  @Post('/addOrUpdateByUserId')
  async addOrUpdateByUserId(@Body() param: any) {
    return this.ok(await this.userProfileProfileService.addOrUpdateByUserId(param));
  }
}
